package com.gdcp.controller.backend;

import com.gdcp.beans.Role;
import com.gdcp.beans.RoleUser;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.service.RoleService;
import com.gdcp.service.RoleUserService;
import com.gdcp.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Controller
@RequestMapping("/render/roleuserManager")
@RolesAllowed({"ROLE_ADMIN"})
public class RoleUserController_backend {

    @Autowired
    RoleUserService roleUserService;
    @Autowired
    CommonCode commonCode;
    @Autowired
    ResponseModel responseModel;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(int pageSize, int pageNumber, String sortName, String sortOrder){
        Map<String,Object> model=commonCode.getList(pageSize, pageNumber, sortName, sortOrder);
        return roleUserService.findPageBreakByCondition(model);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> remove(Integer[] ids){
        if (ids==null){
            return responseModel.setModel(false,"请至少选择一条记录");
        }

        return roleUserService.removeByPrimaryKey(ids);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request){
        if (request.getParameter("iuser_id").equals("")){
            return responseModel.setModel(false,"用户id不能为空");
        }
        if (request.getParameter("irole_id")==null){
            return responseModel.setModel(false,"角色id不能为空");
        }

        int iuser_id = Integer.valueOf(request.getParameter("iuser_id"));
        int irole_id = Integer.valueOf(request.getParameter("irole_id"));

        User user=userService.getInformation(iuser_id);
        if (user==null){
            return responseModel.setModel(false,"用户id不存在");
        }
        //Role role=roleService.getInformation(irole_id);
        //if (role==null){
        //    return responseModel.setModel(false,"角色id不存在");
        //}

        RoleUser roleUser=roleUserService.getInformationByUserId(iuser_id);
        if (roleUser!=null){
            return responseModel.setModel(false,"该用户"+iuser_id+"已经有权限，请去修改(不可再添加)");
        }

        int count=roleUserService.add(iuser_id,irole_id);
        if (count<=0){
            return responseModel.setModel(false,"新增失败，参数错误(注意用户id是外键)");
        }

        return responseModel.setModel(true,"新增成功");
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(HttpServletRequest request) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        if (request.getParameter("iuser_id")==null){
            return responseModel.setModel(false,"用户id不能为空");
        }

        if (request.getParameter("irole_id")==null){
            return responseModel.setModel(false,"角色id不能为空");
        }

        Integer iuser_id = Integer.valueOf(request.getParameter("iuser_id"));
        Integer irole_id = Integer.valueOf(request.getParameter("irole_id"));
        User user=userService.getInformation(iuser_id);
        if (user==null){
            return responseModel.setModel(false,"用户id不存在");
        }
        //RoleUser roleUser1=roleUserService.getInformation(irole_id);
        //if (roleUser1==null){
        //    return responseModel.setModel(false,"角色id不存在");
        //}
        RoleUser roleUser=new RoleUser();
        roleUser.setId(id);
        roleUser.setIuser_id(iuser_id);
        roleUser.setIrole_id(irole_id);
        try{
            return roleUserService.updateInformation(roleUser);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"用户id不存在");
        }
    }

    @PostMapping("/get/{id}")
    @ResponseBody
    public Map<String,Object> get(@PathVariable Integer id){
        RoleUser roleUser= roleUserService.getInformation(id);
        if (roleUser==null){
            return responseModel.setModel(false,"获取失败");
        }

        return responseModel.setModel(true,roleUser);
    }

    @PostMapping("/search")
    @ResponseBody
    public Map<String,Object> search(String searchText){
        String str = "["+searchText+"]";
        JSONArray json= JSONArray.fromObject(str);
        Map<String, Object> param=new HashMap<String, Object>();

        for(int i=0;i<json.size();i++){
            JSONObject jsonOne = json.getJSONObject(i);
            param.put("id", jsonOne.getString("id"));
            param.put("iuser_id", jsonOne.getString("iuser_id"));
            param.put("irole_id", jsonOne.getString("irole_id"));
        }
        return roleUserService.searchInformation(param);
    }
}
